"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.userJoinChatRoom = void 0;
const defines_1 = require("../../lib/defines");
const utils_1 = require("../../utils/utils");
const ChatRoomEntity_1 = require("../../httpServer/entity/ChatRoomEntity");
const log4js_1 = require("log4js");
const logger = (0, log4js_1.getLogger)('socket/adminUpdate');
function userJoinChatRoom(app) {
    const { socket } = app.locals;
    const { userId, username } = socket.handshake.query;
    socket.use(async (event, next) => {
        const [name, params] = event;
        if (name === defines_1.RequestMethod.login) {
            const data = await ChatRoomEntity_1.ChatRoomEntity.createQueryBuilder('member_chat_room')
                .leftJoinAndSelect("member_chat_room.classroom", "classroom")
                .where("member_chat_room.userId=:userId", { userId })
                .getMany();
            if (Array.isArray(data)) {
                for (const person of data) {
                    const { classId, classname } = person.classroom;
                    socket.join(classId);
                    socket.emit('join:chart-room', { classId, classname });
                    logger.debug(`join:(${(0, utils_1.stringify)({ userId, username })}) => 加入聊天室(${(0, utils_1.stringify)({ classId, classname })})`);
                }
            }
        }
        next();
    });
}
exports.userJoinChatRoom = userJoinChatRoom;
